//package com.software.graduationproject.commons.log;
//
//import cn.hutool.extra.servlet.ServletUtil;
//import cn.hutool.http.useragent.UserAgent;
//
//import cn.hutool.http.useragent.UserAgentUtil;
//import com.alibaba.fastjson.JSON;
//import com.software.graduationproject.commons.enums.LogType;
//
//import com.software.graduationproject.commons.util.SecurityUtils;
//import com.software.graduationproject.modules.sys.entity.LoginUser;
//import io.swagger.annotations.ApiOperation;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationEventPublisher;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.lang.reflect.Method;
//
///**
// * LogAspect
// * ---------------------------------------------------------------------------------------------------------------------
// */
//@Aspect
//@Component
//@Slf4j
//public class LogAspect {
//
//    @Autowired
//    private ApplicationEventPublisher applicationEventPublisher;
//
//    @Pointcut("(@annotation(com.software.graduationproject.commons.log.Log) || execution(* com.*..*.*Controller..*(..))) && !execution(* com.*..*.*SysLogController..*(..))")
//    public void logPointCut() {
//    }
//
//    @Around("logPointCut()")
//    public Object around(ProceedingJoinPoint point) throws Throwable {
//        long beginTime = System.currentTimeMillis();
//        Object result;
//        try {
//            result = point.proceed();
//        } catch (Throwable throwable) {
//            throw throwable;
//        } finally {
//            long responseTime = System.currentTimeMillis() - beginTime;
//            MethodSignature signature = (MethodSignature) point.getSignature();
//            Method method = signature.getMethod();
//            ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
//            String requestAction = null;
//            if (apiOperation != null) {
//                requestAction = apiOperation.value();
//            }
//            Log log = method.getAnnotation(Log.class);
//            boolean enabled = true;
//            if (log != null) {
//                enabled = log.enabled();
//            }
//            LogType logType = (log == null) ? LogType.OPERATION : log.logType();
//            boolean ignoreParams = log != null && log.ignoreParams();
//            if (enabled) {
//                LogDto logDto = new LogDto();
//                ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//                HttpServletRequest request = servletRequestAttributes.getRequest();
//                HttpServletResponse response = servletRequestAttributes.getResponse();
//                Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//                if (authentication != null && authentication.isAuthenticated() ) {
//                    LoginUser loginUser = (LoginUser) authentication.getPrincipal();
//                    if (loginUser != null) {
//                        logDto.setUserCode(loginUser.getUsername());
//                        logDto.setUserName(loginUser.getNickName());
//                    } else {
//                        logDto.setUserCode("未知用户");
//                        logDto.setUserName("未知用户");
//                    }
//                } else {
//                    logDto.setUserCode("未认证用户");
//                    logDto.setUserName("未认证用户");
//                }
//                logDto.setRequestIp(ServletUtil.getClientIP(request));
//                logDto.setRequestUrl(request.getRequestURL().toString());
//                logDto.setRequestAction(requestAction);
//                logDto.setRequestMethod(request.getMethod());
//                logDto.setRequestParams(ignoreParams ? "{}" : JSON.toJSONString(point.getArgs()));
//                logDto.setResponseTime(responseTime);
//                logDto.setResponseStatus(response.getStatus());
//                logDto.setLogType(logType);
//                // 获取请求设备信息
//                String userAgentHeader = request.getHeader("User-Agent");
//                UserAgent userAgent = UserAgentUtil.parse(userAgentHeader);
//                String clientDevice = userAgent.getPlatform().getName(); // 获取设备平台信息
//                // 设置设备信息到 logDto
//                logDto.setClientDevice(clientDevice);
//                applicationEventPublisher.publishEvent(new LogEvent(this, logDto));
//            }
//        }
//        return result;
//    }
//}
